package com.ezonsport.android.ui;

import java.util.prefs.Preferences;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.ezonsport.android.EzonSportMobileV2Activity;
import com.ezonsport.android.R;
import com.ezonsport.android.http.HttpException;
import com.ezonsport.android.model.User;
import com.ezonsport.android.task.GenericTask;
import com.ezonsport.android.task.TaskAdapter;
import com.ezonsport.android.task.TaskFeedback;
import com.ezonsport.android.task.TaskListener;
import com.ezonsport.android.task.TaskParams;
import com.ezonsport.android.task.TaskResult;

public class LoginActivity extends Activity {
	private static final String TAG = "LoginActivity";
	private Button mBtnLogin;
	private Button mBtnRegister;
	private EditText mEditUser;
	private EditText mEditPwd;
	private String mUsername;
	private String mPassword;
	
	// Preferences.
	private SharedPreferences mPreferences;

	// Tasks.
	private GenericTask mLoginTask;

	private User user;

	private TaskListener mLoginTaskListener = new TaskAdapter() {

		@Override
		public void onPreExecute(GenericTask task) {
			onLoginBegin();
		}

		@Override
		public void onProgressUpdate(GenericTask task, Object param) {
			
		}

		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			if (result == TaskResult.OK) {
				onLoginSuccess();
			} else {
				onLoginFailure(((LoginTask) task).getMsg());
			}
		}

		@Override
		public String getName() {
			// TODO Auto-generated method stub
			return "Login";
		}
	};
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        mEditUser = (EditText)findViewById(R.id.edit_username);
        mEditPwd = (EditText)findViewById(R.id.edit_password);
        
        mBtnLogin = (Button)findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
		    	intent.setClass(LoginActivity.this, EzonSportMobileV2Activity.class);
		    	startActivity(intent);
				
			}
		});
        mBtnRegister = (Button)findViewById(R.id.btn_register);
        /*mBtnRegister.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//For testing
				String username=m_txtUser.getText().toString();
				//String address="";
				
				
				WSGetUser wsgu=new WSGetUser(username);
				List<String> messages=wsgu.getMessage();//���÷����෽��
				
				Toast.makeText(LoginActivity.this,"�û�������"+ wsgu.getUser().getBrithday().getYear()+"\n"+wsgu.getUser().getBrithday().getMonth()+"\n"+wsgu.getUser().getBrithday().getDay(), Toast.LENGTH_LONG).show();
			}
		});*/
    }
    
    private void onLoginBegin() {
		disableLogin();
		TaskFeedback.getInstance(TaskFeedback.DIALOG_MODE, LoginActivity.this)
				.start(getString(R.string.login_status_logining_in));
	}
    private void onLoginSuccess(){
    	TaskFeedback.getInstance(TaskFeedback.DIALOG_MODE, LoginActivity.this)
			.success("");
		mEditUser.setText("");
		mEditPwd.setText("");
		
		Intent intent = new Intent();
    	intent.setClass(LoginActivity.this, EzonSportMobileV2Activity.class);
    	startActivity(intent);
    }
    private void onLoginFailure(String reason) {
		TaskFeedback.getInstance(TaskFeedback.DIALOG_MODE, LoginActivity.this)
				.failed(reason);
		enableLogin();
	}
    
    
    private class LoginTask extends GenericTask {

		private String msg = getString(R.string.login_status_failure);

		public String getMsg() {
			return msg;
		}

		@Override
		protected TaskResult _doInBackground(TaskParams... params) {
			TaskParams param = params[0];
			//publishProgress(getString(R.string.login_status_logging_in) + "...");

			try {
				String username = param.getString("username");
				String password = param.getString("password");
				//user = EzonSportApplication.mApi.login(username, password);
				
				//EzonSportApplication.getMyselfId(true);
				//EzonSportApplication.getMyselfName(true);
				
			} catch (HttpException e) {
				
				return TaskResult.FAILED;
			}

			//SharedPreferences.Editor editor = mPreferences.edit();
			//editor.putString(Preferences.USERNAME_KEY, mUsername);

			// add 存储当前用户的id
			//editor.putString(Preferences.CURRENT_USER_ID, user.getId());
			//editor.commit();

			return TaskResult.OK;
		}
	}

    
    
    
    
    
    private void disableLogin() {
		mEditUser.setEnabled(false);
		mEditPwd.setEnabled(false);
		mBtnLogin.setEnabled(false);
	}
    private void enableLogin() {
		mEditUser.setEnabled(true);
		mEditPwd.setEnabled(true);
		mBtnLogin.setEnabled(true);
	}
    
    public static String encryptPassword(String password) {
		// return Base64.encodeToString(password.getBytes(), Base64.DEFAULT);
		return password;
	}

	public static String decryptPassword(String password) {
		// return new String(Base64.decode(password, Base64.DEFAULT));
		return password;
	}
    
    
 
    
    
}
